#!/usr/bin/env python2.5
# -*- coding: UTF-8 -*-  
import sys
import os 
 
gdataPath= os.path.dirname(__file__)+'/gdata.zip' 
if not gdataPath in sys.path:sys.path.insert(0, gdataPath)   
#from ..misc import test1 
 
import gdata #@UnresolvedImport : OK does not understand zip
atom=gdata.atom 
service=gdata.service
 
class nmBlogger(object):
    feedsStr='/feeds/' 
    (service,  curBlogId) = (None, None)
    
    def __init__(self,user, pwd, blogName):
        self.service=self.srvBloggerLoginPrg(user, pwd)
        if blogName:self.setCurBlog(blogName)
    @classmethod 
    def srvBloggerLoginPrg(cls, user, pwd):
        srv_blogger = service.GDataService(user, pwd)
        srv_blogger.source = 'nm-App-1.0'
        srv_blogger.service = 'blogger'
        srv_blogger.account_type = 'GOOGLE'
        srv_blogger.server = 'www.blogger.com'
        srv_blogger.ProgrammaticLogin()
        return srv_blogger
    @classmethod 
    def getUserBlogsFeed(cls, srv_blogger):
        query = service.Query()
        query.feed = cls.feedsStr+'default/blogs'
        feed = srv_blogger.Get(query.ToUri())
        return feed    
    def setCurBlog(self, blogName):
        feed=self.getUserBlogsFeed(self.service)
        for entry in feed.entry:
            if entry.title.text==blogName:
                self.curBlogId=entry.GetSelfLink().href.split("/")[-1]
                return self.curBlogId
        self.curBlogId=None
        return None     
    def feedPosts(self):return self.feedsStr + self.curBlogId + '/posts/default'
    def test(self):
        print self.feedPosts()
    def PrintAllPosts(self):
        kar={'max-results':100}
        feed = self.service.GetFeed(self.feedPosts(),kar )  
        print feed.title.text
        for cnt, entry in enumerate( feed.entry):
            print "%4d %s %s" %(cnt, entry.updated.text, entry.title.text ) 
            #print "\t" + entry.content.text 
        return feed
    def PostFeedURI(self):return '/feeds/%s/posts/default' % self.curBlogId      
    def PostCreate(self, title, content, categoriesLst=[], draft=True):
        scheme = 'http://www.blogger.com/atom/ns#'  
        entry = gdata.GDataEntry()
        entry.title = atom.Title('xhtml', title)
        entry.content = atom.Content(content_type='html', text=content)
        new_tags = [atom.Category(term=tag, scheme=scheme) for tag in categoriesLst] 
        entry.category.extend(new_tags)
        if draft:
            control = atom.Control()
            control.draft = atom.Draft(text='yes')
            entry.control = control 
        post= self.service.Post(entry, self.PostFeedURI()) 
        return post
    
    def PostsRetrieve(self):
        feed = self.service.GetFeed(self.PostFeedURI())
        return feed  
        
#def getBlogerFeed(srv_blogger):
#    query = service.Query()
#    query.feed = '/feeds/default/blogs'
#    feed = srv_blogger.Get(query.ToUri())
#    return feed  
    #print feed.title.text 
    #for entry in feed.entry:
        #print "\t" + entry.title.text
    
